package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.AdmissionDto;
import xyz.zhouzhaodong.crm.dto.MailDto;
import xyz.zhouzhaodong.crm.entity.Customer;
import xyz.zhouzhaodong.crm.entity.Mail;
import xyz.zhouzhaodong.crm.mapper.CustomerMapper;
import xyz.zhouzhaodong.crm.mapper.MailMapper;
import xyz.zhouzhaodong.crm.service.IMailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 邮寄 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-19
 */
@Service
public class MailServiceImpl extends ServiceImpl<MailMapper, Mail> implements IMailService {

    @Resource
    MailMapper mailMapper;

    @Resource
    CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addMail(MailDto mailDto) {
        try {
            Mail mail = mailDtoToMail(mailDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            mail.setId(id);
            mail.setCreateUser(mailDto.getStaffId());
            mail.setCreateTime(new Date());
            mailMapper.insert(mail);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteMail(MailDto mailDto) {
        try {
            Mail mail = mailDtoToMail(mailDto);
            mail.setFlag(1);
            mail.setUpdateUser(mailDto.getStaffId());
            mail.setUpdateTime(new Date());
            mailMapper.updateById(mail);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateMail(MailDto mailDto) {
        try {
            Mail mail = mailDtoToMail(mailDto);
            mail.setUpdateUser(mailDto.getStaffId());
            mail.setUpdateTime(new Date());
            mailMapper.updateById(mail);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage selectMail(MailDto mailDto) {
        QueryWrapper<Mail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", mailDto.getCustomerId());
        if(mailDto.getFlag() == null){
            queryWrapper.eq("flag", 0);
        } else {
            queryWrapper.eq("flag", mailDto.getFlag());
        }
        List<Mail> list = mailMapper.selectList(queryWrapper);
        return RestMessage.success(list);
    }

    @Override
    public RestMessage selectMailList(MailDto mailDto) {

        Integer flag = mailDto.getFlag();
        if(flag == null){
            mailDto.setFlag(0);
        }
        Page<MailDto> page = new Page<>(mailDto.getPageNo(),
                mailDto.getPageSize() == 0 ? 10 : mailDto.getPageSize());
        IPage<MailDto> list = mailMapper.selectMailList(mailDto, page);
        for(MailDto a : list.getRecords()){
            Customer customer = customerMapper.selectById(a.getCustomerId());
            a.setCustomer(customer);
        }
        return RestMessage.success(list);
    }

    private Mail mailDtoToMail(MailDto mailDto) {
        Mail mail = new Mail();
        mail.setId(mailDto.getId());
        mail.setCustomerId(mailDto.getCustomerId());
        mail.setMailTime(mailDto.getMailTime());
        mail.setExpressService(mailDto.getExpressService());
        mail.setExpressNo(mailDto.getExpressNo());
        mail.setSignIn(mailDto.getSignIn());
        mail.setVisaStatus(mailDto.getVisaStatus());
        mail.setSigningTime(mailDto.getSigningTime());
        mail.setRemark(mailDto.getRemark());
        mail.setFlag(mailDto.getFlag());
        return mail;
    }
}
